/* Includes ------------------------------------------------------------------*/
#include "lock.h"
#include "icons.h"

/* Private macro -------------------------------------------------------------*/

/* Private types -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/
static lock__box_t lock_box;

/* Private function prototypes -----------------------------------------------*/
void lock_box_btn_cb(lv_event_t *event);

/* Private functions ---------------------------------------------------------*/

/******************************************************************************
 * Function Name  : msgbox_font_init
 * Description    : init message box
 * Input          : [lv_font_t *font_title, lv_font_t *font_text] font
 *                  - [lv_font_t *font_title] title font
 *                  - [lv_font_t *font_tex t] text font
 * Output         : None
 * Return         : None
 * Note           : None
 * Attention      : None
 *******************************************************************************/
void lock_box_font_init(lv_font_t *font_title, lv_font_t *font_text)
{
    lock_box.font_title = font_title;
    lock_box.font_text = font_text;
}

static void txt_lock_input_cb(lv_event_t *event)
{
    lv_event_code_t code = lv_event_get_code(event);
    lv_obj_t *txt = lv_event_get_target(event);
    lv_obj_t *keyboard = lv_event_get_user_data(event);
    // lv_obj_t *parent_box = lv_obj_get_parent(keyboard);
    // lv_obj_t *top_box = lv_obj_get_parent(parent_box);
    lv_obj_t *top_box = lv_layer_top();
    lv_obj_t *box = lv_obj_get_child(top_box, 0);
    if (code == LV_EVENT_FOCUSED)
    {
        // printf("LV_EVENT_FOCUSED \n");
        lv_keyboard_set_textarea(keyboard, txt);
        lv_obj_clear_flag(keyboard, LV_OBJ_FLAG_HIDDEN);
        lv_obj_move_foreground(keyboard);
        lv_obj_set_y(box, 50);
    }
    else if (code == LV_EVENT_DEFOCUSED)
    {
        lv_keyboard_set_textarea(keyboard, NULL);
        lv_obj_add_flag(keyboard, LV_OBJ_FLAG_HIDDEN);
        lv_obj_align_to(lock_box.box, top_box, LV_ALIGN_CENTER, 0, -50);
    }
    else if (code == LV_EVENT_CANCEL || code == LV_EVENT_READY)
    {
        lv_obj_clear_state(txt, LV_STATE_FOCUSED);
        lv_event_send(txt, LV_EVENT_DEFOCUSED, NULL);
    }
}
/******************************************************************************
 * Function Name  : show_msgbox
 * Description    : show message box
 * Input          : [char *title, char *text, bool btn_close_en, bool btn_confirm_en, bool btn_cancel_en, bool btn_back_en, msg_cb_t cb, void *args] title, text, button enable, callback and args
 *                  - [char *title] title
 *                  - [char *text] text
 *                  - [bool btn_close_en] close button enable
 *                  - [bool btn_confirm_en] confirm button enable
 *                  - [bool btn_cancel_en] cancel button enable
 *                  - [bool btn_back_en] back button enable
 *                  - [msg_cb_t cb] callback
 *                  - [void *args] args
 * Output         : None
 * Return         : None
 * Note           : None
 * Attention      : None
 *******************************************************************************/
void show_lock_box(bool btn_close_en, bool btn_confirm_en, bool btn_cancel_en, bool btn_back_en, lock_cb_t cb, void *args)
{
    int btns = 0;
    int btn_width = 100, btn_height = 40;
    lv_obj_t *btn_confirm = NULL;
    lv_obj_t *btn_cancel = NULL;
    lv_obj_t *btn_back = NULL;
    lv_obj_t *top = lv_layer_top();

    lock_box.cb = cb;
    lock_box.args = args;

    lv_obj_add_flag(top, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_set_style_bg_color(top, lv_color_hex(0x000000), 0);
    lv_obj_set_style_bg_opa(top, LV_OPA_50, 0);

    lock_box.box = lv_obj_create(top);
    lv_obj_set_size(lock_box.box, 500, 200);
    lv_obj_set_style_bg_opa(lock_box.box, LV_OPA_COVER, 0);
    lv_obj_align_to(lock_box.box, top, LV_ALIGN_CENTER, 0, -50);
    lv_obj_clear_flag(lock_box.box, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_style_pad_top(top, 50, 0);

    lock_box.keyboard_box = lv_obj_create(top);
    lv_obj_set_size(lock_box.keyboard_box, LV_HOR_RES_MAX, LV_VER_RES_MAX / 2);
    lv_obj_clear_flag(lock_box.keyboard_box, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_style_bg_opa(lock_box.keyboard_box, LV_OPA_0, 0);
    lv_obj_align_to(lock_box.keyboard_box, top, LV_ALIGN_BOTTOM_MID, 0, 0);
    lv_obj_set_style_border_opa(lock_box.keyboard_box, LV_OPA_0, 0);

    lv_obj_t *keyboard = lv_keyboard_create(lock_box.keyboard_box);
    lv_obj_set_style_text_font(keyboard, device.ui.fnt.size_24.font, 0);
    lv_obj_set_size(keyboard, LV_HOR_RES_MAX - 10, LV_VER_RES_MAX / 2 - 10);
    lv_obj_align_to(keyboard, lock_box.keyboard_box, LV_ALIGN_TOP_MID, 0, 0);
    lv_obj_set_style_radius(keyboard, 10, 0);
    lv_obj_add_flag(keyboard, LV_OBJ_FLAG_HIDDEN);

    lv_obj_t *lable_title = lv_label_create(lock_box.box);
    if (lock_box.font_title != NULL)
    {
        lv_obj_set_style_text_font(lable_title, lock_box.font_title, 0);
    }
    lv_label_set_text(lable_title, "输入密码开锁");
    lv_obj_align(lable_title, LV_ALIGN_TOP_MID, 0, 0);

    lv_obj_t *lable_passward = lv_label_create(lock_box.box);
    if (lock_box.font_text != NULL)
    {
        lv_obj_set_style_text_font(lable_passward, lock_box.font_text, 0);
    }
    lv_label_set_text(lable_passward, "密码:");
    // lv_obj_align(lable_lock, LV_ALIGN_CENTER, 0, 0);

    lv_obj_t *txt_password = lv_textarea_create(lock_box.box);
    if (lock_box.font_text != NULL)
    {
        lv_obj_set_style_text_font(txt_password, lock_box.font_text, 0);
    }

    lv_obj_set_style_text_align(txt_password, LV_TEXT_ALIGN_LEFT, 0);
    lv_obj_add_event_cb(txt_password, txt_lock_input_cb, LV_EVENT_ALL, keyboard);
    lv_textarea_set_max_length(txt_password, 31);
    lv_textarea_set_accepted_chars(txt_password, " ()_+-{}[]:<>|ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
    lv_textarea_set_one_line(txt_password, true);

    lv_obj_align_to(lable_passward, lable_title, LV_ALIGN_OUT_BOTTOM_LEFT, -110, 50);
    lv_obj_align_to(txt_password, lable_passward, LV_ALIGN_OUT_RIGHT_MID, 0, 0);

    lock_box.txtarea = txt_password;

    lv_obj_t *line = lv_line_create(lock_box.box);
    static lv_point_t line_points[] = {{0, 0}, {475, 0}};
    lv_line_set_points(line, line_points, 2);
    lv_obj_align_to(line, lable_title, LV_ALIGN_OUT_BOTTOM_MID, 0, 10);

    if (btn_close_en)
    {
        lv_obj_t *btn_close = lv_btn_create(lock_box.box);
        lv_obj_set_size(btn_close, 30, 30);
        lv_obj_set_style_radius(btn_close, 15, 0);
        lv_obj_set_style_border_width(btn_close, 0, 0);
        lv_obj_set_style_pad_all(btn_close, 0, 0);
        lv_obj_set_style_bg_opa(btn_close, LV_OPA_TRANSP, 0);

        lv_obj_t *label_close = lv_label_create(btn_close);
        if (lock_box.font_title != NULL)
        {
            lv_obj_set_style_text_font(label_close, lock_box.font_title, 0);
        }
        lv_label_set_text(label_close, ICON_XMARK);

        lv_obj_align_to(label_close, btn_close, LV_ALIGN_CENTER, 0, 0);
        lv_obj_align_to(btn_close, lock_box.box, LV_ALIGN_TOP_RIGHT, 0, 0);

        lv_obj_add_event_cb(btn_close, lock_box_btn_cb, LV_EVENT_CLICKED, (void *)1);
    }

    if (btn_confirm_en)
    {
        btn_confirm = lv_btn_create(lock_box.box);
        lv_obj_set_size(btn_confirm, btn_width, btn_height);
        lv_obj_set_style_radius(btn_confirm, 5, 0);
        lv_obj_set_style_bg_color(btn_confirm, lv_color_hex(0x000000), 0);
        lv_obj_set_style_border_width(btn_confirm, 0, 0);

        lv_obj_t *label_confirm = lv_label_create(btn_confirm);
        if (lock_box.font_title != NULL)
        {
            lv_obj_set_style_text_font(label_confirm, lock_box.font_title, 0);
        }
        lv_label_set_text(label_confirm, "确定");
        lv_obj_align(label_confirm, LV_ALIGN_CENTER, 0, 0);

        lv_obj_add_event_cb(btn_confirm, lock_box_btn_cb, LV_EVENT_CLICKED, (void *)2);
        btns++;
    }

    if (btn_cancel_en)
    {
        btn_cancel = lv_btn_create(lock_box.box);
        lv_obj_set_size(btn_cancel, btn_width, btn_height);
        lv_obj_set_style_radius(btn_cancel, 5, 0);
        lv_obj_set_style_bg_color(btn_cancel, lv_color_hex(0x000000), 0);
        lv_obj_set_style_border_width(btn_cancel, 0, 0);

        lv_obj_t *label_cancel = lv_label_create(btn_cancel);
        if (lock_box.font_title != NULL)
        {
            lv_obj_set_style_text_font(label_cancel, lock_box.font_title, 0);
        }
        lv_label_set_text(label_cancel, "取消");
        lv_obj_align(label_cancel, LV_ALIGN_CENTER, 0, 0);

        lv_obj_add_event_cb(btn_cancel, lock_box_btn_cb, LV_EVENT_CLICKED, (void *)3);
        btns++;
    }

    if (btn_back_en)
    {
        btn_back = lv_btn_create(lock_box.box);
        lv_obj_set_size(btn_back, btn_width, btn_height);
        lv_obj_set_style_radius(btn_back, 5, 0);
        lv_obj_set_style_bg_color(btn_back, lv_color_hex(0x000000), 0);
        lv_obj_set_style_border_width(btn_back, 0, 0);

        lv_obj_t *label_back = lv_label_create(btn_back);
        if (lock_box.font_title != NULL)
        {
            lv_obj_set_style_text_font(label_back, lock_box.font_title, 0);
        }
        lv_label_set_text(label_back, "返回");
        lv_obj_align(label_back, LV_ALIGN_CENTER, 0, 0);

        lv_obj_add_event_cb(btn_back, lock_box_btn_cb, LV_EVENT_CLICKED, (void *)4);
        btns++;
    }
    if (btns == 1)
    {
        if (btn_confirm_en)
        {
            lv_obj_align_to(btn_confirm, lock_box.box, LV_ALIGN_BOTTOM_MID, 0, 0);
        }
        else if (btn_cancel_en)
        {
            lv_obj_align_to(btn_cancel, lock_box.box, LV_ALIGN_BOTTOM_MID, 0, 0);
        }
        else if (btn_back_en)
        {
            lv_obj_align_to(btn_back, lock_box.box, LV_ALIGN_BOTTOM_MID, 0, 0);
        }
    }
    else if (btns == 2)
    {
        bool second = false;
        if (btn_confirm_en)
        {
            lv_obj_align_to(btn_confirm, lock_box.box, LV_ALIGN_BOTTOM_MID, -btn_width / 2 - 10, 0);
            second = true;
        }
        if (btn_cancel_en)
        {
            if (second)
            {
                lv_obj_align_to(btn_cancel, lock_box.box, LV_ALIGN_BOTTOM_MID, btn_width / 2 + 10, 0);
            }
            else
            {
                lv_obj_align_to(btn_cancel, lock_box.box, LV_ALIGN_BOTTOM_MID, -btn_width / 2 - 10, 0);
                second = true;
            }
        }
        if (btn_back_en)
        {
            lv_obj_align_to(btn_back, lock_box.box, LV_ALIGN_BOTTOM_MID, btn_width / 2 + 10, 0);
        }
    }
    else
    {
        lv_obj_align_to(btn_cancel, lock_box.box, LV_ALIGN_BOTTOM_MID, 0, 0);
        lv_obj_align_to(btn_confirm, btn_cancel, LV_ALIGN_OUT_LEFT_MID, -20, 0);
        lv_obj_align_to(btn_back, btn_cancel, LV_ALIGN_OUT_RIGHT_MID, 20, 0);
    }
}

/******************************************************************************
 * Function Name  : msgbox_btn_cb
 * Description    : message box button callback
 * Input          : [lv_event_t *event] event
 *                  - [lv_event_t *event] event
 * Output         : None
 * Return         : None
 * Note           : None
 * Attention      : None
 *******************************************************************************/
void lock_box_btn_cb(lv_event_t *event)
{
    lv_obj_del(lock_box.keyboard_box);
    lv_obj_t *top = lv_layer_top();
    lv_obj_clear_flag(top, LV_OBJ_FLAG_CLICKABLE);
    lv_obj_set_style_bg_opa(top, LV_OPA_TRANSP, 0);
    int param = (int)lv_event_get_user_data(event);
    if (param == 1)
    {
        // close code callback
    }
    else if (param == 2)
    {
        const char *password_input = lv_textarea_get_text(lock_box.txtarea);
        char password[] = "123456";

        if (strcmp(password_input, password) == 0)
        {
            if (lock_box.cb != NULL)
            {
                lock_box.cb(lock_box.args);
            }
        }
    }
    else if (param == 3)
    {
        // cancel code callback
    }
    else if (param == 4)
    {
        // back code callback
    }
    lv_obj_del(lock_box.box);
}
/************************ (C) COPYRIGHT PURESUN *****END OF FILE***************/
